package com.pucong.gitee.database.backup.business.database.util;

import com.pucong.gitee.database.backup.common.entity.database.dto.DatabaseTableIndexDto;
import com.pucong.gitee.database.backup.common.entity.database.vo.*;

import java.util.List;
import java.util.Map;

public interface OperationService {

    /**
     * 获取数据库list
     *
     * @return
     */
    List<String> getDataBaseList();

    /**
     * 获取模式list
     *
     * @return
     */
    List<String> getSchemaList(String databaseName);

    /**
     * 根据数据源获取所有表名
     *
     * @param databaseName
     * @param schemaName
     * @return
     */
    List<DatabaseTableVo> getTableListByDBInfo(String databaseName, String schemaName);

    /**
     * 获取数据源下的表和字段名称
     *
     * @param databaseName
     * @param schemaName
     * @return
     */
    List<DbTableFiledVo> getTableFiledListByDBInfo(String databaseName, String schemaName);

    /**
     * 根据数据源和表名获取字段信息
     *
     * @param databaseName
     * @param schemaName
     * @param tableName
     * @return
     */
    List<DatabaseTableFiledVo> getTableFiledListByDBInfo(String databaseName, String schemaName, String tableName);

    /**
     * 执行sql文件
     */
    Boolean runSqlByScriptRunner(String sqlPath);

    /**
     * 创建连接
     */
    Boolean getConnection();

    /**
     * 关闭连接
     */
    void closeAll();

    /**
     * 生成分页sql
     *
     * @param sql
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    String generatePageSql(String sql, Integer pageNum, Integer pageSize);

    /**
     * 得到查询总数
     *
     * @param sql
     * @return
     */
    ExeSqlResult getCountByQuerySql(String sql);

    /**
     * 执行更新语句
     *
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql, Object[] params);

    /**
     * 执行查询语句
     *
     * @param sql
     * @param params
     * @return
     */
    public ExeSqlResult executeQuery(String sql, Object[] params);

    /**
     * 执行分页查询语句
     *
     * @param sql
     * @param params
     * @return
     */
    public RunSqlResultPage executeQueryPage(String sql, Object[] params);

    public RunSqlResultPage executeQueryPage(Integer pageNum, Integer pageSize, String sql, Object[] params);

    void initUrl();

    // 停止执行
    void stopExec();

    void setDataBase(String databaseName);

    void setSchema(String schema);

    void trueAutoCommit();

    void falseAutoCommit();

    void commitTransactional();

    void setSessionId(String sessionId);

    String getSessionId();

    Boolean dropDatabase(String name);

    Boolean renameDatabase(String name, String toName);

    Boolean dropDataSchema(String name);

    Boolean renameDataSchema(String name, String toName);

    Boolean dropDataTable(String name);

    Boolean renameDataTable(String name, String toName);

    Boolean addDataTableFiled(String tableName, DatabaseTableFiledVo addDatabaseTableFiledVo);

    /**
     * 删除表
     *
     * @param tableName
     * @return
     */
    Boolean dropTable(String tableName);

    /**
     * 清空表数据
     *
     * @param tableName
     * @return
     */
    Boolean clearTableData(String tableName);

    /**
     * 删除数据
     *
     * @param tableName
     * @param dataList
     * @return
     */
    Boolean deleteTableData(String tableName, List<Map<String, Object>> dataList);

    Boolean deleteDataTableFiled(String tableName, String tableFiled);

    Boolean alertDataTableFiled(String tableName, String tableFiled, DatabaseTableFiledVo toDatabaseTableFiledVo);

    Boolean addFiledPk(String tableName, String tableFiled);

    Boolean deleteFiledPk(String tableName, String tableFiled);

    /**
     * 生成ddl
     *
     * @param tableName
     * @return
     */
    String getDdl(String tableName);

    /**
     * 生成dml
     *
     * @param tableName
     * @return
     */
    String getDml(String tableName);

    /**
     * 字段上移
     *
     * @param tableName
     * @param fileName
     * @return
     */
    Boolean toUp(String tableName, String fileName);

    /**
     * 字段下移
     *
     * @param tableName
     * @param fileName
     * @return
     */
    Boolean toDown(String tableName, String fileName);

    /**
     * 删除字段
     *
     * @param tableName
     * @param fileName
     */
    Boolean deleteFiled(String tableName, String fileName);

    /**
     * 新增字段
     *
     * @param tableName
     * @param filedVo
     */
    Boolean addFiled(String tableName, DatabaseTableFiledVo filedVo);

    /**
     * 修改字段
     *
     * @param tableName
     * @param filedNameOld
     * @param filedVo
     */
    Boolean updateFiled(String tableName, String filedNameOld, DatabaseTableFiledVo filedVo);

    /**
     * 获取字段类型list
     *
     * @return
     */
    String[] getTableFiledTypeList();

    /**
     * 查询表索引信息
     *
     * @param tableName
     * @return
     */
    List<DatabaseTableIndexVo> getTableIndexList(String schemaName, String tableName);

    /**
     * 创建索引
     *
     * @param schemaName
     * @param tableName
     * @param indexDto
     */
    Boolean addIndex(String schemaName, String tableName, DatabaseTableIndexDto indexDto);

    /**
     * 修改索引
     *
     * @param schemaName
     * @param tableName
     * @param indexName
     * @param indexDto
     */
    Boolean updateIndex(String schemaName, String tableName, String indexName, DatabaseTableIndexDto indexDto);

    /**
     * 删除索引
     *
     * @param schemaName
     * @param tableName
     * @param indexName
     */
    Boolean deleteIndex(String schemaName, String tableName, String indexName);

    /**
     * 获取索引函数list
     * @return
     */
    String[] getTableIndexTypeList();

    /**
     * 修改、取消主键
     * @param schemaName
     * @param tableName
     * @param filedName
     * @param pk
     * @return
     */
    Boolean alterPk(String schemaName, String tableName, String filedName, Boolean pk);
}